package com.t194.controller;

import com.t194.service.UserService;
import com.t194.util.Log;
import com.t194.util.PageResult;
import com.t194.util.UserDto;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;

@RestController
@RequestMapping("/sys/user")
public class UserController {
    private String msg;
    @Resource
    private UserService userService;

    @GetMapping("/index")
    public ModelAndView index() {
        ModelAndView mav = new ModelAndView();
        mav.setViewName("sys/user");
        return mav;
    }
    @GetMapping("/query")
    public PageResult query(Integer offset, Integer limit, UserDto userDto) {
        PageResult pageResult = userService.searchPagination(offset, limit, userDto);
        return pageResult;
    }

    @RequestMapping("/grant")
    public String grant(Integer userId, Integer roleId) {
        try {
            userService.grant(userId, roleId);
            msg = "用户授权成功";
        } catch (Exception ex) {
            msg = "用户授权失败";
            Log.logger.error(msg, ex);
        }
        return msg;
    }
}
